package com.uliky.shouyiLibrary.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.uliky.shouyiLibrary.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //在支付宝创建的应用的id
    private   String app_id = "9021000138672771";
    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCoqvto/xhxT0apT1Uhkw2cxkb2QkffsLLkMW63rcZfXf3ecxEvZHQgNQEvV/IJhrntDue3MbyyoQ3yQFfJJDCLhqxnCC/zSOpbKAWeRtGfJCGSVcw4rtcTFjeMdOe/osWsJyqT+8YCSyvTGGDZN5rmgMS/30OSJvmScOBDXgJDkfAq/xSCAqVm7xJier06SNCL9OXpcWTv3eA+1oCsw97gStr0rJRLk9JTGe+ycq03bBMEUdjXj6rmMv52mKtbOd9i3kSDUr7h8vZe8npCEuYPPdcNnvIcTr3pgeNlawrJnQP0Kqq2LKcGh08SgBuuo7luMupMmEApLsxcscw9uDN7AgMBAAECggEBAJRp1009YgGaUz/5yZwAbHLt2VUpX1sKfW7jvFutcidKam+J6q48Ax6v5ToGdbhCL2nTurrs7fB0XPQz/8rCqGCtgRgsER1OpMDPR5pNEHiq70t3aXrbBe8Klphl6eIrVSq1T/blDQfJo2ddRSL9lbSLzBRVXXzGBMcBwaQlnXJcDorTxpqlqFrDhFw9QykzZckmPjIMtaYkRNMmS5piv6bMF1GGt1ia9wtcVoaRADCzi2/ArYA3ECbFqbD1CUBicjFbskdRD97LpynW522U17gmefThitd6TXM5uOs4Ar6nGCHRxovWDoIc3xenYW8YFyc2uQeq3ZFM/luzIFEVobECgYEA89YqFHifcxCtvO49K25TmaOygAyb/nE8MB5fz/pQVAaf82HkmYgWM0md7SkiGFamypBavoQJgHcsZ4bEZU0WHC1ld3Dg2mxdOIHmit/p4nyCn8XuK7WaLBEShiVkYHoZqcYLtOnTHKgslFnbaAdidvmosoxYhRSDeMEhP9iwockCgYEAsRTmia7+Yo8YqRrBUjccWerddwKhSx5HAcf1CIMMESiE7Q/gITqD7BDldObMm3VWN06ihAIdMoSy+maN9X5lqmBcOh5U7yUyNEv+KcsYP/lblH1ysD6hnmW7a/v9bEA+IIg0BwCOtnIINOj+cQI9doEjnte4Sq6N7mJyW1bz7SMCgYAFo/KdPp0RoFUkha5MjuTILQ04ARKqtBvcUReOTBAHoY+Zpa4QDyHUVeOdVNJBFeAN14EB3zQJELiqPGfWoA3e4P3z4VySMOeHIxoeJEfYwWCUKZP01uKWGl7/U2YbBTgKwVYL/Uyw4QMG+30jzufsMKJCXxS9H1hbNrx6KZ0MYQKBgGhw2EuqT1TX6waJxi10XZ9W24m+nlzMnbqaMZX0nKBeEpAzBdDzTy1jBcI+Ab1Kpi/2i/lpuci65Ui0RdPd2Z50OND6ZMOrX4HitCP4KfRJYM8icgsq1W2zuKfGtsAq37mWO4FJveIzPU3fgXorhQ6oudaMiqBZdVJ888+VaEabAoGAdymSfzkgl8di4HCaWwZOPyDwl4/ecRsUlKiyiOxX0Ifmaqm5Q+49HtsM1ZqPNorC+BJe6ZSOG8lXNtbdZWFGlg7clPwrn2j84ViT3ldtlz5LjzjDEqUYgtbZ3lLS0MUxkTQpgMCMYho5Ruli0BZyeqn5X9/kpQYPn6XzfVJtZtA=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv3bysTUx02QAZLSMW6zX7XuTC5/ILS/4tgyRa5V6uyQuLTxXMKdeG7rZEmJzl2wUkfctN8tYDt+DNeMEwnqiv8XcBbXUkOwJAYKVxxKgRiJr0a7h0RD/WLUxX8i7lQNjUETAQZyU7VnYIrwnBSTKpfyTrSzk51DF59z67gx6vbsFVLkTD4gq1ULQb5AyI139k2meNWg84ZTx6Q4oC/PAXvxidv0Jdq2BL34p4we0/nePlTaQ663wnpGi6Tgw3VQzAdqIqVhB7ainVfqnXVZOYikmKI9IsvDCTbGrCMQB+dVkXsAoj45kM8XAstEuBkM/XiJ3LJXx3FrhJNkXPGTLqQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://member.shouyiLibrary.com/memberOrder.html";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://vqmvky.natappfree.cc/payed/notify";
    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"30m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
